const handler = require('sv-handler')

const {
  professionList,
  professionAdd,
  professionUpdate,
  professionDelete
} = require('./module/profession/index.js')

const {
  libList,
  libAdd,
  libUpdate,
  libDelete
} = require('./module/lib/index.js')

const {
  examList,
  examAdd,
  examAddList,
  examUpdate,
  examDelete
} = require('./module/exam/index.js')

const {
  recordList,
  recordAdd,
  favLibList,
  favLibAdd,
  favLibDelete,
  favLibClear,
  buyLibList,
  buyLibAdd,
  favExamList,
  favExamAdd,
  favExamDelete,
  favExamClear,
  errorExamList,
  errorExamAdd,
  errorExamDelete,
  errorExamClear,
  doneExamList,
  doneExamAdd,
  correctNumQuery,
  correctNumInc,
  errorNumQuery,
  errorNumInc
} = require('./module/record/index.js')

const {
  feedbackList,
  feedbackAdd,
} = require('./module/feedback/index.js')

module.exports = {
  _before: async function() { // 通用预处理器
    // token身份安全校验
    // 校验白名单
    const WHITE_LIST = []
    // 校验名单 open easy normal strict 详见handler
    const API_MODE = {
      '/professionList': 'open',
      '/professionAdd': 'strict',
      '/professionUpdate': 'strict',
      '/professionDelete': 'strict',
      '/libList': 'open',
      '/libAdd': 'strict',
      '/libUpdate': 'strict',
      '/libDelete': 'strict',
      '/examList': 'easy',
      '/examAdd': 'strict',
      '/examAddList': 'strict',
      '/examUpdate': 'strict',
      '/examDelete': 'strict',
      '/recordList': 'easy',
      '/recordAdd': 'easy',
      '/favLibList': 'easy',
      '/favLibAdd': 'easy',
      '/favLibDelete': 'easy',
      '/favLibClear': 'easy',
      '/buyLibList': 'easy',
      '/buyLibAdd': 'easy',
      '/favExamList': 'easy',
      '/favExamAdd': 'easy',
      '/favExamDelete': 'easy',
      '/favExamClear': 'easy',
      '/errorExamList': 'easy',
      '/errorExamAdd': 'easy',
      '/errorExamDelete': 'easy',
      '/errorExamClear': 'easy',
      '/doneExamList': 'easy',
      '/doneExamAdd': 'easy',
      '/correctNumQuery': 'easy',
      '/correctNumInc': 'easy',
      '/errorNumQuery': 'easy',
      '/errorNumInc': 'easy',
      '/feedbackList': 'strict',
      '/feedbackAdd': 'easy',
    }

    const apiPath = this.getHttpInfo().path
    // 不是白名单的api需要进行校验
    if (!WHITE_LIST.includes(apiPath)) {
      const cToken = await handler.checkToken({
        clientInfo: this.getClientInfo(),
        token: this.getHttpInfo().headers.authorization,
        mode: API_MODE[apiPath]
      })
      if (cToken.code !== 200) {
        throw cToken
      }
    }

    this.params = {} // 初始化参数
    this.startTime = Date.now() // 在before内记录开始时间并在this上挂载，以供后续流程使用
    // 使用 content-type: application/json 的请求头，参数在body中
    const body = this.getHttpInfo().body
    this.params = JSON.parse(body)
  },
  _after(error, result) {
    if (error) {
      throw error // 如果方法抛出错误，也直接抛出不处理
    }
    result.timeCost = Date.now() - this.startTime
    return result
  },

  /**
   * 职业
   */
  professionList,
  professionAdd,
  professionUpdate,
  professionDelete,

  /**
   * 题库
   */
  libList,
  libAdd,
  libUpdate,
  libDelete,

  /**
   * 题目
   */
  examList,
  examAdd,
  examAddList,
  examUpdate,
  examDelete,

  /**
   * 记录
   */
  recordList,
  recordAdd,
  favLibList,
  favLibAdd,
  favLibDelete,
  favLibClear,
  buyLibList,
  buyLibAdd,
  favExamList,
  favExamAdd,
  favExamDelete,
  favExamClear,
  errorExamList,
  errorExamAdd,
  errorExamDelete,
  errorExamClear,
  doneExamList,
  doneExamAdd,
  correctNumQuery,
  correctNumInc,
  errorNumQuery,
  errorNumInc,

  /**
   * 反馈
   */
  feedbackList,
  feedbackAdd,
}